package cn.wolfcode.redis.service.impl;

import cn.wolfcode.entity.Strategy;
import cn.wolfcode.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.redis.util.RedisKeys;
import cn.wolfcode.redis.vo.StrategyStatisVO;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.util.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void viewnumIncrease(Long id) {
        StrategyStatisVO vo = getStrategyVO(id);
        vo.setViewnum(vo.getViewnum() + 1);
        setStrategyVO(vo);
    }

    @Override
    public void setStrategyVO(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId().toString());
        redisTemplate.opsForValue().set(key, JSON.toJSONString(vo));
    }

    @Autowired
    private IStrategyService strategyService;


    @Override
    public StrategyStatisVO getStrategyVO(Long id) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(id.toString());
        StrategyStatisVO vo;
        if (Boolean.FALSE.equals(redisTemplate.hasKey(key))) {
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.getById(id);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
            redisTemplate.opsForValue().set(key, JSON.toJSONString(vo));
        } else {
            vo = JSON.parseObject(redisTemplate.opsForValue().get(key), StrategyStatisVO.class);
        }
        return vo;
    }

    @Override
    public void replynumIncrease(Long strategyId) {
        StrategyStatisVO vo = getStrategyVO(strategyId);
        vo.setReplynum(vo.getReplynum() + 1);
        setStrategyVO(vo);
    }

    @Override
    public boolean favor(Long sid, Long uid) {
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid.toString());
        List<Long> sidList = null;
        if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) {
            String slist = redisTemplate.opsForValue().get(key);
            sidList = JSON.parseArray(slist, Long.class);
        } else {
            sidList = new ArrayList<>();
        }
        StrategyStatisVO vo = getStrategyVO(sid);
        if (sidList.contains(sid)) {
            // 取消收藏
            sidList.remove(sid);
            vo.setFavornum(vo.getFavornum() - 1);
        } else {
            sidList.add(sid);
            vo.setFavornum(vo.getFavornum() + 1);
        }
        redisTemplate.opsForValue().set(key, JSON.toJSONString(sidList));
        setStrategyVO(vo);
        return sidList.contains(sid);
    }

    @Override
    public boolean hasFavor(Long sid, Long userId) {
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(userId.toString());
        List<Long> sidList = null;
        if (Boolean.FALSE.equals(redisTemplate.hasKey(key))) {
            return false;
        }
        List<Long> sids = JSON.parseArray(redisTemplate.opsForValue().get(key), Long.class);
        if (sids == null || sids.isEmpty() || !sids.contains(sid)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean strategyThumbup(Long sid, Long uid) {
        String key = RedisKeys.STRATEGY_THUMBSUP.join(uid.toString()).join(sid.toString());
        if (Boolean.FALSE.equals(redisTemplate.hasKey(key))) {
            StrategyStatisVO vo = getStrategyVO(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            setStrategyVO(vo);
            // 设置过期时间
            Date now = new Date();
            Date endDate = DateUtil.getEndDate(now);
            long time = DateUtil.getDateBetween(now, endDate);
            redisTemplate.opsForValue().set(key, "1", time, TimeUnit.SECONDS);
            return true;
        }
        return false;
    }

    @Override
    public boolean isVoExists(Long id) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(id.toString());
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    @Override
    public List<StrategyStatisVO> queryVOByPattern(String s) {
        Set<String> keys = redisTemplate.keys(RedisKeys.STRATEGY_STATIS_VO.join(s));
        ArrayList<StrategyStatisVO> vos = new ArrayList<>();
        if (keys != null && keys.size() > 0) {
            for (String key : keys) {
                String value = redisTemplate.opsForValue().get(key);
                StrategyStatisVO vo = JSON.parseObject(value, StrategyStatisVO.class);
                vos.add(vo);
            }
        }
        return vos;
    }
}
